package com.chenchao.thread;

import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @encoding : UTF-8
 * @Time : 2020/11/17 21:19
 * @Author : Chen Chao
 * @File : javaSE.com.chenchao.javathread.Test16.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 * CopyOnWriteArrayList安全线程
 */
public class Test16 implements Runnable {
    public static void main(String[] args) throws InterruptedException {
        //ArrayList<String> strings = new ArrayList<>();
        CopyOnWriteArrayList<String> strings = new CopyOnWriteArrayList<>();
        //CopyOnWriteArrayList安全线程，结果是10000
        for (int i = 0; i < 100000; i++) {
            new Thread(()->{
                strings.add(Thread.currentThread().getName());
            }).start();
        }
        Thread.sleep(10000);
        //停10秒是为了防止有些线程还没有搞完，
        //ArrayList最终结果仍然小于10000，说明线程不安全
        System.out.println(strings.size());
    }
    @Override
    public void run() {

    }
}
